// 模拟数据
// import menus from "./mock/menus.js.bak";
import menus from "./mock/menus";
import menus_sec from "./mock/menus_sec";
import menus_adu from "./mock/menus_adu";
import user from "./mock/user";
import router from "../router";

//模拟数据登录用户角色
const role = "";

//后台接口数据
const menuUrl = `/api/upms/sysMenu`;
const userUrl = `/api/upms/sysUser/info`;
const developer = window.PUZZLE_CONFIG.developer
import httpUtils from "@/core/utils/http.utils";

/**
 * 获取用户数据
 * @returns {Promise<unknown>}
 */
export function getUser() {

    if (developer == "debug") {
        //获取后端数据
        return getUserInfo();
    } else {
        return new Promise(resolve =>
            setTimeout(() => {
                if (sessionStorage.getItem("frame"))
                    user.frame = JSON.parse(sessionStorage.getItem("frame"));
                resolve(user);
            }, 100)
        );
    }
}

/**
 * 获取菜单数据
 * @returns {Promise<unknown>}
 */
export function getMenus() {

    //菜单结构数据获取
    if (developer == "debug") {
        //获取后端数据
        return getMenusInfo();
    } else {
        //获取前端mock菜单数据
        return new Promise((resolve) => {
            setTimeout(() => {
                if (role == "settings") {
                    resolve({
                        data: menus_adu
                    })
                } else if (role == "settings-sec") {
                    resolve({
                        data: menus_sec
                    })
                } else {
                    resolve({
                        data: menus
                    })
                }
            }, 100);
        });

    }
}

/**
 * 获取后端菜单数据
 * @returns {Promise<unknown>}
 */
function getMenusInfo() {
    if (window.sessionStorage.getItem('token')) {
        const TENANT =  window.sessionStorage.getItem('TENANT-ID')
        return new Promise((resolve, reject) => {
            httpUtils.get(menuUrl, {"tenantId": TENANT}).then((response) => {
                resolve({
                    data: response.data
                });
            }).catch(function (error) {
                sessionStorage.removeItem("token");
                sessionStorage.removeItem("TENANT-ID");
                sessionStorage.removeItem("refresh_token");
                sessionStorage.removeItem("username");
                sessionStorage.removeItem("userId");
                sessionStorage.removeItem("permissions");
                sessionStorage.removeItem("tabList");
                resolve({
                    data: [{}]
                })
            });
        });

    } else {
        return new Promise((resolve, reject) => {
            resolve({
                data: [{}]
            });
        });
    }

}

/**
 * 获取当前登录用户信息
 * @returns {Promise<unknown>}
 */
function getUserInfo() {

    if (window.sessionStorage.getItem('token')) {
        const TENANT =  window.sessionStorage.getItem('TENANT-ID')
        return new Promise((resolve, reject)=>{
            let userInfo = JSON.parse(window.sessionStorage.getItem('allUserInfo'))
            // console.log(userInfo);
            let rolecodes = userInfo.roleCodes;
                        // 110 sysadmin 角色编码
            if(rolecodes){
                if(rolecodes.indexOf("110", 0)>-1){
                    sessionStorage.setItem("isGetCpuAndMemorry", 1);
                }else{
                    sessionStorage.setItem("isGetCpuAndMemorry", 0);
                }
            }
             resolve(userInfo);
        })
        // return new Promise((resolve, reject) => {
        //     httpUtils.get(userUrl,{"tenantId": TENANT}).then((response) => {
        //         if(response.code !== 10200){
        //             sessionStorage.clear();
        //             router.push("/login");
        //         }else {
        //             // console.log(response.data);
        //             sessionStorage.setItem("permissions", JSON.stringify(response.data.permissions));
        //             sessionStorage.setItem("isTipPassUpdate", JSON.stringify(response.data.isTipPassUpdate));
        //             //根据角色判断是否添加开启查询内存cpu信息的定时器
        //             let rolecodes = response.data.roleCodes;
        //             // 110 sysadmin 角色编码
        //             if(rolecodes){
        //                 if(rolecodes.indexOf("110", 0)>-1){
        //                     sessionStorage.setItem("isGetCpuAndMemorry", 1);
        //                 }else{
        //                     sessionStorage.setItem("isGetCpuAndMemorry", 0);
        //                 }
        //             }
        //             resolve(response.data);
        //         }

        //     }).catch(function (error) {
        //         sessionStorage.clear();
        //         router.push("/login");
        //         resolve({
        //             data: [{}]
        //         })
        //     });
        // });

    } else {
        return new Promise((resolve, reject) => {
            resolve({
                data: [{}]
            });
        });
    }

}
